package cloud.demarcia.yzcrawler.utils;


import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


@Slf4j
public class Browser {

    private WebClient webClient;

    private void clientConfig() {
        webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.waitForBackgroundJavaScript(2000);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//设置支持AJAX
        webClient.getCache().setMaxSize(100);
    }


    public Document browse(String urlString) {
        clientConfig();
        HtmlPage page = null;
        try {
            page = webClient.getPage(urlString);
            Thread.sleep(2000);
        } catch (Exception e) {
            log.error("FAILED TO GET PAGE, ERROR PAGE: " + urlString + "!");
        }
        if (page == null) {
            log.error("FAILED TO GET PAGE, ERROR PAGE: " + urlString + "!");
        }
        assert page != null;
        return Jsoup.parse(page.asXml());
    }

    public void close() {
        webClient.getCache().clear();
        webClient.getCurrentWindow().getJobManager().removeAllJobs();
        webClient.close();
    }
}
